//
//  B_CollectionViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_CollectionViewController.h"
#import "B_CollectionListTVC.h"
#import "B_CollectionListModel.h"
#import "B_TopicDetailVC.h"

@interface B_CollectionViewController ()
@property (nonatomic, strong) BaseTableView * mainView;
@end

@implementation B_CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的收藏"];
    self.view = self.mainView;
    [self getData:YES];
}


- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView setTableViewFrameWithTop:0 left:0 right:0 bottom:0];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_CollectionListTVC" bundle:nil] forCellReuseIdentifier:@"B_CollectionListTVC"];
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 160;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_CollectionListModel * model = weSelf.dataArray[indexPath.row];
            B_CollectionListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_CollectionListTVC" forIndexPath:indexPath];
            cell.B_titleLabel.text = model.title;
            cell.B_zanLabel.text = [NSString stringWithFormat:@"%@赞 · %@评论", safeValue1(model.likeCount), safeValue1(model.topicCommentCount)];
            cell.B_timeLabel.text = [NSObject getTimeTo:model.createTime];
            cell.B_userName.text = model.nickName;
            [cell.B_userImageView sd_setImageWithURL:[NSURL URLWithString:model.portrait]];
            [cell.B_collectionImageView sd_setImageWithURL:[NSURL URLWithString:model.picture]];
            if ([model.userType isEqualToString:@"2"]) {
                cell.B_userIcon.hidden = NO;
                cell.B_userIcon.image = [UIImage imageNamed:@"B_personal_center_real_name_user_icon"];
            } else if ([model.userType isEqualToString:@"3"]) {
                /// 商家
                cell.B_userIcon.hidden = NO;
                cell.B_userIcon.image = [UIImage imageNamed:@"B_personal_center_merchants_icon"];
            } else {
                cell.B_userIcon.hidden = YES;
            }
            
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            //TODO: 话题详情界面
            B_CollectionListModel * model = weSelf.dataArray[indexPath.row];
            B_TopicDetailVC * topicDetail = [[B_TopicDetailVC alloc] init];
            topicDetail.topicId = model.id;
            [weSelf pushNextViewController:topicDetail];
            
        };
        [_mainView setTableViewHeaderFresh:^{
            [weSelf getData:YES];
        }];
        [_mainView setTableViewFooterFresh:^{
            [weSelf getData:NO];
        }];
        
    }
    return _mainView;
}

- (void) getData:(BOOL) isRefresh {
    
    if (isRefresh) {
        self.index = 1;
    } else {
        self.index ++;
    }
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/topiccollect/selectUserTopicCollectList/%@/%ld/10", getUserId(), (long)self.index] parameters:nil isCache:isRefresh succeed:^(id data) {
        [weSelf.mainView endRefresh];
        if ([data[@"code"] isEqualToString:@"20010"]) {
            if (isRefresh) {
                [weSelf.dataArray removeAllObjects];
            }
            for (NSDictionary * dictionary in data[@"values"]) {
                B_CollectionListModel * model = [B_CollectionListModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [weSelf.mainView endRefresh];
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
